package com.ck.ips.service.impl.sys;

import com.ck.common.model.OperationResult;
import com.ck.common.model.Sysconfig;
import com.ck.core.constant.CacheConstant;
import com.ck.core.constant.SysConfigNameEnum;
import com.ck.ips.dao.sys.SysConfigMapper;
import com.ck.ips.model.vo.sys.SysConfigVO;
import com.ck.ips.service.sys.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: huangf
 * @date: 2019/5/5 13:55
 */
@Service("sysconfigService")
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    @Cacheable(value = CacheConstant.CACHE_NAME_CK_SPRING_CACHE_SYS_CONFIG,key = "#config.getType()",unless="#result == null" )
    public Sysconfig getByType(SysConfigNameEnum config) {
        Sysconfig sysconfig = sysConfigMapper.getByType(config.getType());
        return sysconfig;
    }

    @Override
    public List<SysConfigVO> getAllConfig() {
        return sysConfigMapper.getAllConfig();
    }

    @Override
    @CachePut(value=CacheConstant.CACHE_NAME_CK_SPRING_CACHE_SYS_CONFIG,key = "#sysconfig.getType()",condition = "#sysconfig.getValue()!=null")
    @Transactional
    public OperationResult updateConfig(Sysconfig sysconfig) {

        if ( StringUtils.isBlank(sysconfig.getType()) || StringUtils.isBlank(sysconfig.getValue()) ) {
            return OperationResult.newValidateFailOperationResult("配置信息不能为空");
        }
        SysConfigNameEnum config = SysConfigNameEnum.getByType(sysconfig.getType());
        if ( config == null ) {
            return OperationResult.newValidateFailOperationResult("配置信息不存在");
        }
        sysConfigMapper.updateConfig( sysconfig.getType(), sysconfig.getValue() );
        return OperationResult.newSuccessTipsOperationResult("修改系统配置：【"+config.getDesc()+"】");
    }

    @Override
    // @CacheEvict(value="product",key="#id")
    // @CacheEvict(value="product",allEntries = true) //清除所有缓存
    // beforeInvocation = true 的含义：
    //               // 缺省为 false，即缺省情况下，都是在实际的方法执行完成后，才对缓存进行清空操作。
    //               // 期间如果执行方法出现异常，则会导致缓存清空不被执行
    @CacheEvict(value=CacheConstant.CACHE_NAME_CK_SPRING_CACHE_SYS_CONFIG,key = "#type",beforeInvocation = true)
    public OperationResult delConfig(String type) {
        SysConfigNameEnum config = SysConfigNameEnum.getByType(type);
        if ( config == null ) {
            return OperationResult.newValidateFailOperationResult("配置信息不存在");
        }
        sysConfigMapper.delConfig(type);
        return OperationResult.newSuccessTipsOperationResult("删除系统配置：【"+config.getDesc()+"】");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value=CacheConstant.CACHE_NAME_CK_SPRING_CACHE_SYS_CONFIG,allEntries = true,beforeInvocation = true)
    public OperationResult batchUpdate(List<Sysconfig> configs) {
        sysConfigMapper.batchUpdate(configs);
        return OperationResult.newSuccessTipsOperationResult("修改系统基础设置");
    }
}
